package com.easymin.weixin.controller.res;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.easymin.weixin.common.CommonUtil;
import com.easymin.weixin.common.DataGrid;
import com.easymin.weixin.common.api.DaijiaApi;
import com.easymin.weixin.entity.Order;
import com.easymin.weixin.entity.WxApplication;
import com.easymin.weixin.manager.ErrandManager;
import com.easymin.weixin.manager.WxApplicationManager;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("api/errand")
public class ErrandOrderRes {

	private static final Logger logger = LoggerFactory.getLogger(ErrandOrderRes.class);

	@Autowired
	private ErrandManager errandManager;

	@Autowired
	private WxApplicationManager wxApplicationManager;

	@Autowired
	private DaijiaApi daijiaApi;

	/**
	 * 第一版本的查询会员信息,会员绑定微信,跑腿下单.
	 * 
	 * @param title
	 *            任务标题
	 * @param content
	 *            任务内容
	 * @param deliverTime
	 *            交付时间 2015-09-22 10:57
	 * @param deliverAddress
	 *            交付地址
	 * @param deliverLat
	 *            交付地址纬度
	 * @param deliverLng
	 *            交付地址经度
	 * @param passengerId
	 *            客户Id
	 * @param phone
	 *            客户电话
	 * @param channelName
	 *            订单来源渠道名
	 * @param shouldPay
	 *            订单定价
	 */
	@ResponseBody
	@RequestMapping(value = { "create" }, method = RequestMethod.POST)
	public Map<String, Object> createOrder(@RequestParam("title") String title, @RequestParam("content") String content,
			@RequestParam("deliverTime") String deliverTime, @RequestParam("deliverAddress") String deliverAddress,
			@RequestParam("deliverLat") Double deliverLat, @RequestParam("deliverLng") Double deliverLng,
			@RequestParam(required = false, value = "passengerId") Long passengerId,
			@RequestParam("phone") String phone, @RequestParam("channelName") String channelName,
			@RequestParam(required = false, value = "companyId") Long companyId,
			@RequestParam(required = false, value = "companyName") String companyName,
			@RequestParam(required = false, value = "shouldPay") Double shouldPay,
			@RequestParam("appKey") String appKey) throws ParseException {
		Map<String, Object> map = new HashMap<String, Object>();
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
		String string = daijiaApi.findErrandSetting(wxApplication.getAddress());

		JSONObject jsonObject = JSON.parseObject(string);

		Boolean errandFixPrice = jsonObject.getBooleanValue("errandFixPrice");
		try {
			map = errandManager.createOrder(title, content, deliverTime, deliverAddress, deliverLat, deliverLng,
					errandFixPrice, shouldPay, passengerId, phone, channelName, null, companyId, companyName, null,
					null, null, null, null, null, null, null, null, null, null, null, appKey);
			Long orderId = (Long) map.get("orderId");
			if (orderId != null && orderId != 0) {
				// 推送到系统下单信息
				daijiaApi.pushNewOrder(wxApplication.getAddress(), orderId);
			}

		} catch (Exception ex) {
			logger.error(ex.getMessage());
		}
		return map;
	}

	/**
	 * 预算跑腿订单价格.
	 */

	@RequestMapping(value = { "getPrice" }, method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> getPrice(@RequestParam("passengerId") Long passengerId,
			@RequestParam("city") String city, @RequestParam("acKey") String acKey,
			@RequestParam("startLat") Double startLat, @RequestParam("startLng") Double startLng,
			@RequestParam("endLat") Double endLat, @RequestParam("endLng") Double endLng,
			@RequestParam("appKey") String appKey) throws ParseException {
		Map<String, Object> map = new HashMap<String, Object>();
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
		try {
			map = daijiaApi.getErrandPrice(wxApplication.getAddress(), passengerId, city, acKey, startLat, startLng,
					endLat, endLng);
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			map.put("code", 500);
			map.put("msg", "预算失败");
		}
		return map;
	}

	/**
	 * 预算跑腿订单价格.
	 */
	@RequestMapping(value = { "getNewPrice" }, method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> getNewPrice(@RequestParam("passengerId") Long passengerId,
			@RequestParam("city") String city, @RequestParam(required = false, value = "acKey") String acKey,
			@RequestParam("time") Integer time, @RequestParam("mileage") Double mileage,
			@RequestParam("appKey") String appKey) throws ParseException {
		Map<String, Object> map = new HashMap<String, Object>();
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
		try {
			map = daijiaApi.getErrandNewPrice(wxApplication.getAddress(), passengerId, city, acKey, time, mileage);
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			map.put("code", 500);
			map.put("msg", "预算失败");
		}
		return map;
	}

	/**
	 * 第二版跑腿下单接口.
	 * 
	 * @param title
	 *            任务标题
	 * @param content
	 *            任务内容
	 * @param deliverTime
	 *            交付时间
	 * @param deliverAddress
	 *            交付地址
	 * @param deliverLat
	 *            交付地址纬度
	 * @param deliverLng
	 *            交付地址经度
	 * @param passengerId
	 *            客户Id
	 * @param passengerPhone
	 *            客户电话
	 * @param channelName
	 *            订单来源渠道名
	 * @param companyId
	 *            公司Id
	 * @param companyName
	 *            公司名称
	 * @param shouldPay
	 *            订单定价
	 * @param startTime
	 *            开始时间
	 * @param startPhone
	 *            开始电话
	 * @param endPhone
	 *            结束电话
	 * @param startAddress
	 *            开始地址
	 * @param startLat
	 *            开始地经纬度
	 * @param startLng
	 *            开始地经纬度
	 * @param orderType
	 *            订单类型
	 * @param photo1
	 *            描述图片
	 * @param startPrice
	 *            起步价格
	 * @param mileagePrice
	 *            行驶里程价格
	 * @param travelTimePrice
	 *            服务时间价格
	 * @param appKey
	 *            区分系统
	 */
	@ResponseBody
	@RequestMapping(value = { "createNewOrder" }, method = RequestMethod.POST)
	public Map<String, Object> createNewOrder(@RequestParam("title") String title,
			@RequestParam("content") String content, @RequestParam("deliverTime") String deliverTime,
			@RequestParam("deliverAddress") String deliverAddress, @RequestParam("deliverLat") Double deliverLat,
			@RequestParam("deliverLng") Double deliverLng,
			@RequestParam(required = false, value = "passengerId") Long passengerId,
			@RequestParam("phone") String passengerPhone, @RequestParam("channelName") String channelName,
			@RequestParam(required = false, value = "companyId") Long companyId,
			@RequestParam(required = false, value = "companyName") String companyName,
			@RequestParam(required = false, value = "shouldPay") Double shouldPay,
			@RequestParam(required = false, value = "startTime") String startTime,
			@RequestParam(required = false, value = "startPhone") String startPhone,
			@RequestParam(required = false, value = "endPhone") String endPhone,
			@RequestParam(required = false, value = "startAddress") String startAddress,
			@RequestParam(required = false, value = "startLat") Double startLat,
			@RequestParam(required = false, value = "startLng") Double startLng,
			@RequestParam(required = false, value = "orderType") String orderType,
			@RequestParam(required = false, value = "travelTime") Integer travelTime,
			@RequestParam(required = false, value = "mileage") Double mileage,
			@RequestParam(required = false, value = "startPrice") Double startPrice,
			@RequestParam(required = false, value = "mileagePrice") Double mileagePrice,
			@RequestParam(required = false, value = "travelTimePrice") Double travelTimePrice,
			@RequestParam(required = false, value = "photo1") MultipartFile photo1,
			@RequestParam(required = false, value = "photo2") MultipartFile photo2,
			@RequestParam(required = false, value = "photo3") MultipartFile photo3,
			@RequestParam(required = false, value = "photo4") MultipartFile photo4,
			@RequestParam(required = false, value = "photo5") MultipartFile photo5, HttpServletRequest req,
			@RequestParam("appKey") String appKey) throws ParseException {
		Map<String, Object> map = new HashMap<String, Object>();
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
		if (StringUtils.isBlank(orderType)) {
			orderType = "other";
		}
		try {
			map = errandManager.createOrder(title, content, deliverTime, deliverAddress, deliverLat, deliverLng, null,
					shouldPay, passengerId, passengerPhone, channelName, null, companyId, companyName, startTime,
					startPhone, endPhone, startAddress, startLat, startLng, orderType, startPrice, mileagePrice,
					travelTimePrice, travelTime, mileage, appKey);
			Long orderId = (Long) map.get("orderId");
			try {
				String basePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort();
				String address = wxApplication.getAddress();
				String imgs = "";
				if (photo1 != null) {
					String imgUrl = CommonUtil.uploadErrandImg(photo1, req);
					// 保存图片到代驾系统
					String img = daijiaApi.uploadImg(address, orderId, basePath + "/" + imgUrl);
					imgs = img;
				}
				if (photo2 != null) {
					String imgUrl = CommonUtil.uploadErrandImg(photo2, req);
					String img = daijiaApi.uploadImg(address, orderId, basePath + "/" + imgUrl);
					imgs += "," + img;
				}
				if (photo3 != null) {
					String imgUrl = CommonUtil.uploadErrandImg(photo3, req);
					String img = daijiaApi.uploadImg(address, orderId, basePath + "/" + imgUrl);
					imgs += "," + img;
				}
				if (photo4 != null) {
					String imgUrl = CommonUtil.uploadErrandImg(photo4, req);
					String img = daijiaApi.uploadImg(address, orderId, basePath + "/" + imgUrl);
					imgs += "," + img;
				}
				if (photo5 != null) {
					String imgUrl = CommonUtil.uploadErrandImg(photo5, req);
					String img = daijiaApi.uploadImg(address, orderId, basePath + "/" + imgUrl);
					imgs += "," + img;
				}
				if (StringUtils.isNotBlank(imgs)) {
					errandManager.saveOrderImgs(orderId, channelName, imgs, appKey);
				}
			} catch (Exception ex) {
				logger.error(ex.getMessage());
				return map;
			}
			if (orderId != null && orderId != 0) {
				// 推送到系统下单信息
				daijiaApi.pushNewOrder(wxApplication.getAddress(), orderId);
			}

		} catch (Exception ex) {
			logger.error(ex.getMessage());
		}
		return map;
	}

	/**
	 * 修改订单状态.
	 * 
	 * @param orderId
	 *            订单ID
	 * @param employId
	 *            司机(员工)ID
	 * @param employName
	 *            司机名字
	 * @param action
	 *            接单:"jiedan",拒单:"refuse",执行:"run",销单:"cancel",完单:"finish"
	 * @param operator
	 *            操作人
	 * @param cause
	 *            销单原因
	 * @param appKey
	 *            区分系统
	 */
	@ResponseBody
	@RequestMapping(value = { "updateStatus" }, method = RequestMethod.POST)
	public Map<String, Object> updateStatus(@RequestParam("orderId") Long orderId,
			@RequestParam(required = false, value = "employId") Long employId,
			@RequestParam(required = false, value = "employName") String employName,
			@RequestParam(required = false, value = "employPhone") String employPhone,
			@RequestParam(required = false, value = "employCompanyId") Long employCompanyId,
			@RequestParam("action") String action, @RequestParam("operator") String operator,
			@RequestParam(required = false, value = "cause") String cause, @RequestParam("appKey") String appKey) {
		Map<String, Object> map = new HashMap<String, Object>();
		appKey = StringUtils.trimToEmpty(appKey);
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
		try {
			Order order = errandManager.findOne(orderId, appKey);
			if (order == null) {
				map.put("code", -1);
				map.put("msg", "订单不存在!");
				return map;
			} else if ("cancel".equals(action) && order.getStatus() > 0) {
				map.put("code", -1);
				map.put("msg", "订单已开始执行,不能销单!");
				return map;
			} else if ("jiedan".equals(action)) {
				boolean bool = false;
				bool = daijiaApi.findEmployStatus(wxApplication.getAddress(), employId);
				if (!bool) {
					map.put("code", -1);
					map.put("msg", "司机忙碌,暂时不能接单");
					return map;
				}
			}
			if ("cancel".equals(action)) {
				employId = order.getEmployId();
			}
			String result = errandManager.updateStatus(orderId, employId, employName, employPhone, employCompanyId,
					action, operator, cause, appKey);
			if (StringUtils.isBlank(result)) {
				map.put("code", 0);
				map.put("msg", "成功");
			} else {
				map.put("code", -1);
				map.put("msg", "操作失败");
			}
			return map;
		} catch (HttpClientErrorException ex) {
			logger.error(ex.getMessage());
			map.put("code", 500);
			map.put("msg", "操作失败");
			return map;
		} catch (HttpServerErrorException ex) {
			logger.error(ex.getMessage());
			map.put("code", 500);
			map.put("msg", "操作失败");
			return map;
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			map.put("code", 500);
			map.put("msg", "操作失败");
			return map;
		}

	}

	/**
	 * 第一版本开始抢单,添加抢单记录.
	 */

	@RequestMapping(value = { "toGrabOrder" }, method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> toGrabOrder(@RequestParam("orderId") Long orderId,
			@RequestParam("employId") Long employId, @RequestParam("employName") String employName,
			@RequestParam("employPhone") String employPhone, @RequestParam("employCompanyId") Long employCompanyId,
			@RequestParam("distance") Double distance, @RequestParam("appKey") String appKey) {
		Map<String, Object> map = new HashMap<String, Object>();
		map = errandManager.toGrabOrder(orderId, employId, employName, employPhone, employCompanyId, distance, appKey);
		return map;
	}

	/**
	 * 第一版本抢单人数量.
	 */
	@RequestMapping(value = { "grabTime" }, method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> grabTime(@RequestParam("orderId") Long orderId,
			@RequestParam("appKey") String appKey) {
		Map<String, Object> map = new HashMap<String, Object>();
		map = errandManager.grabTime(orderId, appKey);
		return map;
	}

	/**
	 * 第一版本获取抢单结果.
	 */
	@RequestMapping(value = { "grabOrder" }, method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> grabOrder(@RequestParam("orderId") Long orderId,
			@RequestParam("employId") Long employId, @RequestParam("appKey") String appKey) {
		Map<String, Object> map = new HashMap<String, Object>();
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
		String string = daijiaApi.findErrandSetting(wxApplication.getAddress());
		JSONObject jsonObject = JSON.parseObject(string);
		Integer errandGrabAmount = jsonObject.getInteger("errandGrabAmount");
		map = errandManager.grabOrder(orderId, employId, errandGrabAmount, appKey);
		return map;
	}

	/**
	 * 第二版本跑腿抢单接口.
	 */
	@RequestMapping(value = "grab", method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> grabNewOrder(@RequestParam("orderId") Long orderId,
			@RequestParam("employId") Long employId, @RequestParam("employName") String employName,
			@RequestParam("employPhone") String employPhone, @RequestParam("employCompanyId") Long employCompanyId,
			@RequestParam("distance") Double distance, @RequestParam("appKey") String appKey) {
		Map<String, Object> map = new HashMap<String, Object>();
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
		String string = daijiaApi.findErrandSetting(wxApplication.getAddress());
		JSONObject jsonObject = JSON.parseObject(string);
		Integer errandGrabAmount = jsonObject.getInteger("errandGrabAmount");
		map = errandManager.grab(employName, employPhone, distance, orderId, employId, employCompanyId,
				errandGrabAmount, appKey);
		return map;
	}

	/**
	 * 查询单个订单.
	 */
	@RequestMapping(value = { "findOne" }, method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> findOne(@RequestParam("orderId") Long orderId,
			@RequestParam("appKey") String appKey) {

		Map<String, Object> map = new HashMap<String, Object>();
		appKey = StringUtils.trimToEmpty(appKey);

		try {
			Order order = errandManager.findOne(orderId, appKey);
			map.put("code", 0);
			map.put("msg", "成功");
			map.put("data", order);
			return map;
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			map.put("code", 500);
			map.put("msg", ex.getMessage());
			return map;
		}

	}

	/**
	 * 根据乘客Id,查询订单接口.
	 * 
	 * @param status
	 *            "present":当前订单(0:新单1:已派单2:已接单3:执行中),
	 *            "noreview"未评价(6:已完成(未评价)),"already"已完成 (4:已完成) 5:已销单
	 */
	@RequestMapping(value = { "findOrder" }, method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> findByParams(@RequestParam("passengerId") Long passengerId,
			@RequestParam("page") Integer page, @RequestParam("rows") Integer rows,
			@RequestParam("status") String status, @RequestParam("appKey") String appKey) {

		Map<String, Object> map = new HashMap<String, Object>();
		appKey = StringUtils.trimToEmpty(appKey);

		try {
			DataGrid<Map<String, Object>> list = new DataGrid<Map<String, Object>>();
			list = errandManager.findOrder(passengerId, page, rows, status, appKey);

			map.put("code", 0);
			map.put("msg", "成功");
			map.put("data", list);
			return map;

		} catch (Exception ex) {
			logger.error(ex.getMessage());
			map.put("code", 500);
			map.put("msg", "加载数据失败");
			return map;
		}
	}

	/**
	 * 评价订单.
	 */
	@RequestMapping(value = { "review" }, method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> review(@RequestParam("orderId") Long orderId,
			@RequestParam("score") Double score, @RequestParam(required = false, value = "content") String content,
			@RequestParam("appKey") String appKey) {
		Map<String, Object> map = new HashMap<String, Object>();
		appKey = StringUtils.trimToEmpty(appKey);
		try {
			String result = errandManager.review(orderId, score, content, appKey);

			if (StringUtils.isBlank(result)) {
				map.put("code", 0);
				map.put("msg", "评价成功");
			} else {
				map.put("code", -1);
				map.put("msg", "评价失败");
			}
			return map;
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			map.put("code", 500);
			map.put("msg", "评价失败");
			return map;
		}

	}

	/**
	 * 查询最新订单.
	 */
	@RequestMapping(value = { "findNewOrder" }, method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> findNewOrder(@RequestParam("page") Integer page,
			@RequestParam("rows") Integer rows, @RequestParam("appKey") String appKey) {
		Map<String, Object> map = new HashMap<String, Object>();
		appKey = StringUtils.trimToEmpty(appKey);
		try {
			DataGrid<Map<String, Object>> dataGrid = new DataGrid<Map<String, Object>>();
			dataGrid = errandManager.findNewOrder(page, rows, appKey);
			map.put("code", 0);
			map.put("msg", "成功");
			map.put("data", dataGrid);
			return map;
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			map.put("code", 500);
			map.put("msg", "加载数据失败");
			return map;
		}
	}

	/**
	 * 查询司机端当前任务.
	 */
	@RequestMapping(value = { "findMyWork" }, method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> findMyWork(@RequestParam("employId") Long employId,
			@RequestParam(required = false, value = "longitude") Double longitude,
			@RequestParam(required = false, value = "latitude") Double latitude, @RequestParam("page") Integer page,
			@RequestParam("rows") Integer rows, @RequestParam("appKey") String appKey) {
		Map<String, Object> map = new HashMap<String, Object>();
		appKey = StringUtils.trimToEmpty(appKey);
		try {
			DataGrid<Map<String, Object>> dataGrid = new DataGrid<Map<String, Object>>();
			dataGrid = errandManager.findMyWork(employId, longitude, latitude, page, rows, appKey);
			map.put("code", 0);
			map.put("msg", "成功");
			map.put("data", dataGrid);
			return map;
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			map.put("code", 500);
			map.put("msg", "加载数据失败");
			return map;
		}
	}

	/**
	 * 查询司机端附近订单.
	 * 
	 * @param longitude
	 *            交付地点经度
	 * @param latitude
	 *            交付地点纬度
	 */
	@RequestMapping(value = { "findNearOrder" }, method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> findNearOrder(@RequestParam("longitude") Double longitude,
			@RequestParam("latitude") Double latitude, @RequestParam("page") Integer page,
			@RequestParam("rows") Integer rows, @RequestParam("appKey") String appKey) {
		Map<String, Object> map = new HashMap<String, Object>();
		appKey = StringUtils.trimToEmpty(appKey);
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
		try {
			String string = daijiaApi.findErrandSetting(wxApplication.getAddress());
			JSONObject jsonObject = JSON.parseObject(string);

			Double distance = jsonObject.getDouble("nearPaotuiOrdersKm");
			DataGrid<Map<String, Object>> dataGrid = new DataGrid<Map<String, Object>>();
			dataGrid = errandManager.findNearOrder(longitude, latitude, distance, page, rows, appKey);
			map.put("code", 0);
			map.put("msg", "成功");
			map.put("data", dataGrid);
			return map;
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			map.put("code", 500);
			map.put("msg", "加载数据失败");
			return map;
		}
	}

	/**
	 * 查询三个司机端列表的订单数量.
	 */
	@RequestMapping(value = { "queryCount" }, method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> queryCount(@RequestParam("employId") Long employId,
			@RequestParam("longitude") Double longitude, @RequestParam("latitude") Double latitude,
			@RequestParam("distance") Double distance,
			@RequestParam(required = false, value = "appKey") String appKey) {
		Map<String, Object> map = new HashMap<String, Object>();
		appKey = StringUtils.trimToEmpty(appKey);
		Map<String, Object> countMap = new HashMap<String, Object>();
		if (StringUtils.isBlank(appKey)) {
			countMap.put("newOrder", 0);
			countMap.put("nearOrder", 0);
			countMap.put("myWork", 0);
			map.put("code", 0);
			map.put("msg", "成功");
			map.put("data", countMap);
			return map;
		}
		try {
			countMap = errandManager.queryCount(employId, longitude, latitude, distance, appKey);
			map.put("code", 0);
			map.put("msg", "成功");
			map.put("data", countMap);
			return map;
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			map.put("code", 500);
			map.put("msg", "统计失败");
			map.put("data", "");
			return map;
		}
	}

	/**
	 * 统计客户端的三个列表的数字.
	 */
	@RequestMapping(value = { "queryOrders" }, method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> queryOrders(@RequestParam("passengerId") Long passengerId,
			@RequestParam("appKey") String appKey) {
		Map<String, Object> map = new HashMap<String, Object>();
		appKey = StringUtils.trimToEmpty(appKey);
		try {
			Map<String, Object> countMap = new HashMap<String, Object>();
			countMap = errandManager.queryOrders(passengerId, appKey);

			map.put("code", 0);
			map.put("msg", "成功");
			map.put("data", countMap);
			return map;
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			map.put("code", 500);
			map.put("msg", "统计失败");
			map.put("data", "");
			return map;
		}
	}

	/**
	 * 获取收费标准(显示)的接口.
	 */
	@RequestMapping(value = { "findErrandPay" }, method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> findErrandPay(@RequestParam("city") String city,
			@RequestParam(required = false, value = "acKey") String acKey, @RequestParam("appKey") String appKey) {

		Map<String, Object> map = new HashMap<String, Object>();
		appKey = StringUtils.trimToEmpty(appKey);

		try {

			String content = errandManager.findErrandPay(city, acKey, appKey);
			map.put("code", 0);
			map.put("msg", "成功");
			map.put("data", content);
			return map;
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			map.put("code", 500);
			map.put("msg", "失败");
			map.put("data", "");
			return map;
		}
	}

	/**
	 * 催一下订单进度.
	 * 
	 * @param orderId
	 *            订单Id
	 */
	@RequestMapping(value = { "reminder" }, method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> reminder(@RequestParam("orderId") Long orderId,
			@RequestParam("appKey") String appKey) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();

		appKey = StringUtils.trimToEmpty(appKey);
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);

		Order order = errandManager.findOne(orderId, appKey);
		if (null == order) {
			map.put("code", -1);
			map.put("msg", "订单不存在");
			return map;
		}
		if (order.getStatus() == 4 || order.getStatus() == 5) {
			map.put("code", -1);
			map.put("msg", "订单已完成,或已销单,催促无效");
			return map;
		}
		if (order.getLasturge() != null) {
			if ((new Date().getTime() - order.getLasturge().getTime()) < (2 * 60 * 1000)) {
				map.put("code", -1);
				if (order.getCreated().getTime() == order.getLasturge().getTime()) {
					map.put("msg", "我们正在为您寻找服务人员");
				} else {
					map.put("msg", "订单刚催促过,两分钟内不能重复操作");
				}
				return map;
			}
		}

		try {

			daijiaApi.hastenOrder(wxApplication.getAddress(), orderId); // 调用代驾的接口让其显示消息

			String content = errandManager.hasten(orderId, appKey); // 调用跑腿系统更新上次催促时间
			if ("success".equals(content)) {
				map.put("code", 0);
				map.put("msg", "消息已发出,管理员已经知道了");
			} else if ("fail".equals(content)) {
				map.put("code", -1);
				map.put("msg", "操作失败");
			}
			return map;
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			map.put("code", 500);
			map.put("msg", "催促操作失败");
			return map;
		}
	}
}
